package com.coinland.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.coinland.study.R;
import com.coinland.study.base.BaseMvpActivity;
import com.coinland.study.bean.DocumentBean;
import com.coinland.study.util.AssetsFileUtil;
import com.coinland.study.util.Contant;
import com.coinland.study.util.ToastUtil;
import com.coinland.study.view.download.DownloadContract;
import com.coinland.study.view.download.DownloadPresenter;
import com.coinland.study.widgt.ProgressBarView;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;


/**
 * 类描述:  文件下载页面
 * 创建人:  shenZ
 * 创建时间: 2020/4/14
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/4/14
 */
public class DownloadFileActivity extends BaseMvpActivity<DownloadPresenter>
        implements DownloadContract.View {

    @BindView(R.id.tv_sub)
    public TextView tvSub;

    @BindView(R.id.tv_title)
    public TextView tvTitle;

    @BindView(R.id.iv_doc)
    public ImageView ivDoc;

    @BindView(R.id.et_url)
    public EditText etUrl;

    @BindView(R.id.progressBar)
    public ProgressBarView progressBarView;

    @BindView(R.id.tv_docName)
    public TextView tvDocName;

    private File savaFile;
    private Context mContext;

    //搜索按钮
    @OnClick({R.id.iv_back, R.id.tv_sub, R.id.iv_doc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_sub:
//                String url = etUrl.getText().toString();
//                savaFile = new File(getDocPath(), "test.doc"); //获取文件路径
//                mPresenter.downFile(url, savaFile, subscriber);

                // 将assets文件写入指定文件夹
                String fileName = "test.docx";
                AssetsFileUtil.putAssetsToSDCard(mContext, "docx");

                // 将assets文件写入指定文件夹
                String fileName2 = "“磨题帮App”使用教程.doc";
                AssetsFileUtil.putAssetsToSDCard(mContext, "doc");

                break;
            case R.id.iv_doc:
//                if (savaFile.exists()) {
//                    Intent i = new Intent(DownloadFileActivity.this, TabsReaderViewActivity.class);
//                    i.putExtra(TabsReaderViewActivity.DOC_PATH, savaFile.toString());
//                    startActivity(i);
//                }

                // 模拟添加多个文档
                List<DocumentBean> paths = spUtils.getList(mContext, Contant.DOC_PATH, DocumentBean.class);
                if (paths != null && paths.size() < 5) {  // 最多添加5个
                    DocumentBean documentBean = new DocumentBean();
                    documentBean.setLocalPath(getExternalFilesDir("doc").getAbsolutePath() + "/“磨题帮App”使用教程.doc");
                    documentBean.setTitle("“磨题帮App”使用教程");
                    paths.add(documentBean);
                    spUtils.saveList(mContext, Contant.DOC_PATH, paths);
                }

                Intent i = new Intent(DownloadFileActivity.this, TabsReaderViewActivity.class);
                i.putExtra(Contant.DOC_PATH, getExternalFilesDir("docx").getAbsolutePath() + "/test.docx");
                i.putExtra(Contant.DOC_NAME, "test");
                startActivity(i);
                break;
        }
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        mContext = DownloadFileActivity.this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_download_file;
    }

    @Override
    public void initView() {
        tvTitle.setText("WORD文档下载");
        tvSub.setVisibility(View.VISIBLE);
        tvSub.setText("下载");
        etUrl.setText(Contant.DOWNLOADPATH);

        mPresenter = new DownloadPresenter();
        mPresenter.attachView(this);
    }

    @Override
    public void downLoading(int i) {
        progressBarView.setProgress(i);
    }

    @Override
    public void downFial() {
        ToastUtil.showShort("下载失败", DownloadFileActivity.this);
    }

    @Override
    public void setMax(int l) {
        progressBarView.setMax(l);
    }

    @Override
    public void downFinish() {
        String assetsPath = savaFile.getAbsolutePath();
        String name = assetsPath.substring(assetsPath.lastIndexOf('/'));
        tvDocName.setText(name);
        ToastUtil.showShort("下载成功", DownloadFileActivity.this);
    }

    Subscriber subscriber = new Subscriber() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.e("Presenter", e.getMessage());
        }

        @Override
        public void onNext(Object o) {

        }
    };


    //文档存储路径
    public String getDocPath() {
        String directoryPath = "";
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {//判断外部存储是否可用
            directoryPath = getExternalFilesDir("docx").getAbsolutePath();
        } else {//没外部存储就使用内部存储
            directoryPath = getFilesDir() + File.separator + "docx";
        }
        File file = new File(directoryPath);
        Log.e("测试路径", directoryPath);
        if (!file.exists()) {//判断文件目录是否存在
            file.mkdirs();
        }
        return directoryPath;
    }


    // -------------- 不做处理  --------------

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }


}
